package com.example.makotoshoda.myapplication;

import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.List;


public class RowActivity extends ActionBarActivity {

    private String name;
    private int mId;
    private MyDB mDb;
    ContentValues values;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_row);
        Intent intent = getIntent();
        mId = intent.getIntExtra("id", 0);
        mDb = new MyDB(this);
        List<ContentValues> user = mDb.getUser(mId);
        if (user.size() == 0) {
            setName("未登録");
            return;
        }

        values = user.get(0);
        ((Button)findViewById(R.id.btnUpdate)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText name = (EditText) findViewById(R.id.editName);
                mDb.saveUser(name.getText().toString(), values.getAsInteger("counter"), values.getAsInteger("sort"), mId);
                finish();
            }
        });

        ((Button)findViewById(R.id.btnDelete)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDb.deleteUser(mId);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_row, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void setName(String name) {
        this.name = name;
        ((EditText) findViewById(R.id.editName)).setText(name);
    }
}
